The tag syntax in WebWork is extremely easy to understand. To quickly get started, all you need to know is that all attributes are applied as Strings initially. They are then parsed for the syntax %{ ... }, and anything in between the braces is evaluated against the value stack.
Like most things in life, it turns out that this isn't quite that simple. Specifically, there are actually three rules to be aware of:
The best way to understand these rules is by looking at some examples.
Some ExamplesThe most basic example explaining how the tag syntax works is as follows. This example shows off rule #1 only: <ww:textfield label="%{getText("state.label")}" name="state"/>
In this example, the label is dynamically evaluated and set to the outcome of the OGNL expression getText("state.label"), which will in turn invoke the Internationalization system are retrieve the value of the i18n key state.label. The name, being a String attribute, is simply set to the string state. The next example shows off rule #2: <ww:select label="%{getText("state.label")}" name="state" multiple="true"/>
While this looks very similar to the last example, the key thing to recognize is that the multiple attribute is of type Boolean, which means it falls under rule #2. Generally you won't even notice this, because true as an OGNL expression evaluated to true, which is what you want. Now let's suppose we want to extend this example to show off rule #3 by making the multiple attribute dynamic: <ww:select label="%{getText("state.label")}" name="state" multiple="%{allowMultiple}"/>
Because the attribute is of type Boolean and starts and ends with the correct characters from rule #3, it is reduced to the expression allowMultiple, which is evaluated against the value stack, returning a true of false value, just like in the previous example. There is one trick example to keep an eye on, however. For example, the following is probably incorrect: <ww:textfield label="%{getText("state.label")}" name="state" value="CA"/>
This example will only work if the expression CA can result in something, meaning that your action has a method getCA(), which is probably not what you expected. This is because the value attribute is of type Object and therefore rule #2 applies. If the desire is to set a static String as the initial value, you would need to supply an OGNL expression that returns a String. For example, this is the correct way to do it: <ww:textfield label="%{getText("state.label")}" name="state" value="%{'CA'}"/>
While you could set the value attribute as just "'CA'", we recommend the parsed expressions so that, in the future when WebWork supports parsed attributes for all types, your code will still work. |